package com.jinzhi.jzweb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzweb.dao.JazSysContractTemplateDao;
import com.jinzhi.jzweb.domain.JazSysContractTemplateDO;
import com.jinzhi.jzweb.domain.JazSysProjectDO;
import com.jinzhi.jzweb.service.JazSysContractTemplateService;
import com.jinzhi.jzweb.service.JazSysProjectService;
import org.springframework.stereotype.Service;


import com.jinzhi.common.base.CoreServiceImpl;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <pre>
 *
 * </pre>
 * <small> 2023-04-18 09:11:44 | Long</small>
 */
@Service
public class JazSysContractTemplateServiceImpl extends CoreServiceImpl<JazSysContractTemplateDao, JazSysContractTemplateDO> implements JazSysContractTemplateService {

    private final JazSysProjectService jazSysProjectService;

    public JazSysContractTemplateServiceImpl(JazSysProjectService jazSysProjectService) {
        this.jazSysProjectService = jazSysProjectService;
    }

    @Override
    public Page<JazSysContractTemplateDO> getSysContractPage(Integer pageSize, Integer curr) {
        Page<JazSysContractTemplateDO> jazSysContractDOPage = selectPage(new Page<>(
                Optional.of(curr).orElse(1),
                Optional.of(pageSize).orElse(10)));
        //查询项目类型
        List<String> projectIds = jazSysContractDOPage.getRecords().stream().filter(a -> Objects.nonNull(a.getProjectId())).
                map(JazSysContractTemplateDO::getProjectId).distinct().collect(Collectors.toList());
        if (ObjectUtils.isEmpty(projectIds)) {
            return jazSysContractDOPage;
        }
        List<String> projectIds1 = new ArrayList<>();
        for (int i = 0; i < projectIds.size(); i++) {
            projectIds1.addAll(Arrays.asList(projectIds.get(i).split(",")));
        }
        List<JazSysProjectDO> jazSysProjectDOS = jazSysProjectService.selectList(new EntityWrapper<JazSysProjectDO>().in("id", projectIds1));
        jazSysContractDOPage.getRecords().forEach(a -> {
            a.setProjectName(jazSysProjectDOS.stream().filter(b -> Objects.nonNull(a.getProjectId())).
                    filter(b -> a.getProjectId().contains(b.getId().toString())).map(JazSysProjectDO::getName).collect(Collectors.joining(",")));
        });
        return jazSysContractDOPage;
    }

    @Override
    public JazSysContractTemplateDO getSysContractById(Long id) {
        JazSysContractTemplateDO jazSysContractDO = selectById(id);
        if (StringUtil.isNotNull(jazSysContractDO.getProjectId())) {
            return jazSysContractDO;
        }
        List<String> projectIds1 = Arrays.asList(jazSysContractDO.getProjectId().split(","));
        List<JazSysProjectDO> jazSysProjectDOS = jazSysProjectService.selectList(new EntityWrapper<JazSysProjectDO>().in("id", projectIds1));
        if (jazSysProjectDOS.isEmpty()) {
            return jazSysContractDO;
        }
        StringBuffer pro = new StringBuffer();
        jazSysProjectDOS.forEach(b -> {
            if (jazSysContractDO.getProjectId().contains(b.getId().toString())) {
                jazSysContractDO.setProjectName(pro.append(b.getName() + "、").toString());
            }
        });
        return jazSysContractDO;
    }
}
